cmake_minimum_required(VERSION 3.10)
project(Watch)


set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_SIZE arm-none-eabi-size)

set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release")
set(MCU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(CMAKE_C_FLAGS "${MCU_FLAGS} -w -Wno-unknown-pragmas -ffunction-sections -fdata-sections")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g1 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-Os")


add_definitions(
-DSTM32F401xC
-DUSE_FULL_LL_DRIVER
-DUSE_HAL_DRIVER
-DARM_MATH_CM4
)


################################配置文件#################################
option(EN_FREERTOS "use FreeRTOS" ON)
option(EN_LIB_LVGL "use lvgl" ON)
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
    "${CMAKE_CURRENT_BINARY_DIR}/config.h"
)
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

################################项目文件#################################
include_directories(
    Port/stm32f4/lib/CMSIS/Device/ST/STM32F4xx/Include
    Port/stm32f4/lib/CMSIS/Driver/ST/STM32F4xx/Include
    Port/stm32f4/lib/CMSIS/Include
    Port/stm32f4/lib/ST/STM32F4xx/Include
    Port/stm32f4/lib/STM32F4xx_HAL_Driver/Inc
    Port/stm32f4/lib/STM32F4xx_HAL_Driver/Inc/Legacy
    Port/stm32f4/bsp
    Driver
    Src
    Src/App/gui
    Src/App/os
    
)

add_subdirectory(
    Src
)
set(SRCS ${SRCS}
${USER_SRCS}
)

add_subdirectory(
    Driver
)
set(SRCS ${SRCS}
${DRIVER_SRCS}
)

#MCU库
add_subdirectory(
    Port/stm32f4/lib
)
set(SRCS ${SRCS}
${STM32_SRCS}
)

add_subdirectory(
    Port/stm32f4/bsp
)
set(SRCS ${SRCS}
${STM32_BSP_SRCS}
)

#设置.s文件使用c编译器编译
set(SRCS ${SRCS}
Port/stm32f4/lib/startup_stm32f401xc.s
)
set_property(SOURCE Port/stm32f4/lib/startup_stm32f401xc.s PROPERTY LANGUAGE C)

################################可选模块#################################


#FreeRTOS
if (EN_FREERTOS)
    include_directories(
        Middlewares/Third_Party/FreeRTOS/CMSIS_RTOS_V2
        Middlewares/Third_Party/FreeRTOS/include
        Middlewares/Third_Party/FreeRTOS/portable/GCC/ARM_CM4F
    )
    add_subdirectory(
        Middlewares/Third_Party/FreeRTOS
    )
    set(SRCS ${SRCS}
    ${SRCS_FREERTOS}
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/App/os/freertos.c
    )
endif (EN_FREERTOS)

#LVGL
if (EN_LIB_LVGL)
    include_directories(
        Middlewares/Third_Party/LVGL
        Middlewares/Third_Party/LVGL/lvgl_port
        Middlewares/Third_Party/LVGL/lvgl
        Middlewares/Third_Party/LVGL/lvgl/src

    )
    add_subdirectory(
        Middlewares/Third_Party/LVGL
    )
    set(SRCS ${SRCS}
    ${SRCS_LVGL}
    )
endif (EN_LIB_LVGL)






################################编译选项#################################
#链接属性
set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/Port/stm32f4/lib/STM32F401CCUx_FLASH.ld)
set(CMAKE_EXE_LINKER_FLAGS
"--specs=nano.specs -specs=nosys.specs -nostartfiles -T${LINKER_SCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections"
)

#链接库
link_libraries(m)

#编译结果
add_executable(${PROJECT_NAME}.elf ${SRCS})


set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE}
    COMMAND ${CMAKE_OBJCOPY} -Oihex  ${ELF_FILE} ${HEX_FILE}
    COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"

    COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex"
    COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin"

    COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
    COMMENT "Invoking: Cross ARM GNU Print Size"
)